package com.co.notaweb.util;

import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import com.co.notaweb.beans.system.LoginBean;
import com.co.notaweb.beans.system.Principal;
import com.co.notaweb.constantes.ConstantesNav;
import com.co.notaweb.entities.Persona;

public class FacesUtils {
	private static ResourceBundle rb;
	private static FacesContext context;
	static {
		rb = ResourceBundle.getBundle("com.co.notaweb.messages.messages");
		context = FacesContext.getCurrentInstance();
	}

	public static void addSuccesMessage(String message, Object... params) {
		FacesMessage fMessage = new FacesMessage(FacesMessage.SEVERITY_INFO,
				message, null);
		context.addMessage(null, fMessage);

	}

	public static void addErrorMessage(String message) {
		FacesMessage fMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR,
				message, null);
		context.addMessage(null, fMessage);
	}

	public static void addWarnMessage(String message) {
		FacesMessage fMessage = new FacesMessage(FacesMessage.SEVERITY_WARN,
				message, null);
		context.addMessage(null, fMessage);
	}

	public static void addSuccesMessageBundle(String message, Object... params) {
		FacesMessage fMessage = new FacesMessage(FacesMessage.SEVERITY_INFO,
				rb.getString(message), null);
		context.addMessage(null, fMessage);
	}

	public static void addErrorMessageBundle(String message) {
		FacesMessage fMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR,
				rb.getString(message), null);
		context.addMessage(null, fMessage);
	}

	public static void addWarnMessageBundle(String message) {
		FacesMessage fMessage = new FacesMessage(FacesMessage.SEVERITY_WARN,
				rb.getString(message), null);
		context.addMessage(null, fMessage);
	}

	@SuppressWarnings("unchecked")
    public static <T> T findBean(String beanName) {
        FacesContext context = FacesContext.getCurrentInstance();
        return (T) context.getApplication().evaluateExpressionGet(context, "#{" + beanName + "}", Object.class);
    }
	
	public static Persona getPersona(){
		LoginBean login = findBean(ConstantesNav.BEAN_LOGIN);
		return login.getPersona();
	}

}
